home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1 / Ian and Stuart's One (Australia).iso / Australasian Legends / Commercial / Rainbow Hill / MacDOS™ 2.0.0 / batches / existVar.bat < prev    next >
DOS Batch File  |  1994-08-26  |  2KB  |  47 lines

  1. @echo off
  2. !  existVar.bat   Batch file to check/ensure that a variable exists
  3. !
  4. !  existVar name default
  5. !
  6. !  where 'name' is the name of the variable and default is the value to
  7. !  which the variable should be set if it does not exists.
  8. !  This batch is useful because MacDOS (like DOS) removes a user defined
  9. !  variable when it gets assigned the empty string. As a consequence, a
  10. !  check like   if %var% == "whatever" goto LBL   generates an error when
  11. !  the variable does not exist.
  12. !
  13. !  The simplest way to check for existence of a variable is as follows:
  14. !    if not "%varName%" == %%VARNAME%% goto EXISTS_LBL
  15. !  This check works because a reference to a non-existing variable returns
  16. !  a capitalised version of the reference itself. So, if the variable varName
  17. !  does not exist, an attempt to de-reference it returns the string %VARNAME% .
  18. !  The double percent signs are necessary to represent one '%'.
  19. !
  20. !  Unfortunately, you cannot derefrence twice a variable. That is, you cannot
  21. !  extract the value from a variable whose name is stored in another variable.
  22. !  Therefore, the simple 'if' shown above cannot be used in a batch like this
  23. !  in which the name of a variable is passed as an argument. The way to do it
  24. !  here is to use the internal checking of MacDOS.
  25. !
  26. !  This batch files lets you assign a default to a variable and it ensures
  27. !  that the variable exists. This of course is only true if the second
  28. !  parameter is defined!
  29. !
  30.  
  31.     !
  32.     ! append a character to the variable and then remove it. MacDOS will
  33.     ! reports error 63 if the variable does not exist. To keep it simple,
  34.     ! we assume that the variable does not exist if ANY error occurs.
  35.     !
  36.     onerror VAR_ERR_LBL
  37.     incr %1 by "*"
  38.     decr %1
  39.     goto DONE_LBL
  40.  
  41. :VAR_ERR_LBL
  42.     !
  43.     ! The variable does not exist. Create it by setting it to the requested default.
  44.     set %1=%2
  45.  
  46. :DONE_LBL
  47.